package com.jt.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jt.mapper.CollectMapper;
import com.jt.mapper.CourseMapper;
import com.jt.pojo.Collect;
import com.jt.pojo.Course;
import com.jt.vo.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;

@Service
public class CollectServiceImpl implements CollectService{

    @Autowired
    private CollectMapper collectMapper;


    @Override
    public PageResult getCollectList(PageResult pageResult) {
        //1.构建分页对象  参数1: 第几页   参数2: 多少条
        Page<Collect> page = new Page<>(pageResult.getPageNum(),pageResult.getPageSize());
        //2.准备条件构造器 构建模糊查询
        QueryWrapper queryWrapper = new QueryWrapper();
        String query = pageResult.getQuery();
        boolean flag = StringUtils.hasLength(query);
        queryWrapper.like(flag,"favorite_name",query);
        //3.根据MP查询 实现分页数据的自动封装
        page = collectMapper.selectPage(page,queryWrapper);
        //4.获取数据,返回分页对象
        long total = page.getTotal();
        //获取分页结果
        List<Collect> rows = page.getRecords();
        return pageResult.setTotal(total).setRows(rows);
    }

    @Override
    @Transactional //控制事务
    public void updateCollectStatus(Collect collect) {
        collectMapper.updateById(collect);
    }

    @Override
    @Transactional //控制事务
    public void deleteCollectById(Collect collect) {
        collectMapper.deleteById(collect.getCollectId());
    }

    @Override
    @Transactional //控制事务
    public void updateCollectById(Collect collect) {
        collectMapper.updateById(collect);
    }

    @Override
    public void saveCollect(Collect collect) {
        collectMapper.insert(collect);
    }


}
